﻿using Fuyao.Business.Entities.Models;
using Fuyao.Business.Logic.BC;
using Fuyao.Win.UI.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Fuyao.Win.UI.Util;
using Fuyao.Win.UI;

namespace Fuyao.Win.UI
{
    public partial class frmPrincipal : Form
    {
        BusinessLogic bc = new BusinessLogic();

        #region Properties
        public Usuario LoggedUser { get; set; }
        #endregion

        public frmPrincipal(Usuario oLoggedUser)
        {
            InitializeComponent();
            this.LoggedUser = oLoggedUser;            
        }

        private void cargarMenuDinamico()
        {
            this.rPrincipal.Size = new System.Drawing.Size(750, 130);
            List<Fuyao.Business.Entities.Models.Menu> listMenu = bc.getMenuWithChildByPerfilID(this.LoggedUser.PerfilID);
            ResourceManager rm = Resources.ResourceManager;
            foreach (Fuyao.Business.Entities.Models.Menu oMenu in listMenu)
            {
                RibbonTab rtab = new System.Windows.Forms.RibbonTab();
                rtab.Text = oMenu.NombreMenu;

                RibbonPanel rpnl;
                if (oMenu.SubMenuList != null)
                {
                    foreach (SubMenu oSubMenu in oMenu.SubMenuList)
                    {
                        if (oSubMenu.TieneAcceso)
                        {
                            rpnl = new System.Windows.Forms.RibbonPanel();
                            rpnl.Text = oSubMenu.NombreSubMenu;
                            rpnl.Tag = oSubMenu.MenuID;

                            RibbonButton rbtn = new System.Windows.Forms.RibbonButton();
                            rbtn.Image = (System.Drawing.Image)rm.GetObject(oSubMenu.NombreImagen);
                            rbtn.Click += rbtn_Click;
                            rbtn.Tag = oSubMenu;

                            rpnl.Items.Add(rbtn);
                            rtab.Panels.Add(rpnl);
                        }
                    }
                }
                if (oMenu.TieneAcceso)
                    rPrincipal.Tabs.Add(rtab);
            }
        }


        private void rbtn_Click(object sender, EventArgs e)
        {
            RibbonButton rbtn = (RibbonButton)sender;
            SubMenu oSubMenuTemp = (SubMenu)rbtn.Tag;
            if (oSubMenuTemp.MenuID == 4) instanciarFrmLogin();
            else setChildPosition(oSubMenuTemp.NombreFormulario);
        }

        private void instanciarFrmLogin()
        {
            cerrarFormAbiertos();
            desactivaMenu(false);
            var _frmLogin = new frmLogin();
            _frmLogin.ShowDialog();
            LoggedUser = _frmLogin.oUsuario;
            if (_frmLogin.DialogResult == DialogResult.OK)
            {
                eliminarAllMenu();
                cargarMenuDinamico();
            }
            else desactivaMenu(true);
        }

        private void eliminarAllMenu()
        {           
            for (int i = 0; i < rPrincipal.Tabs.Count; i++)
            {
                rPrincipal.Tabs.Remove(rPrincipal.Tabs[i]);
                i--;
            }
        }

        private void desactivaMenu(bool _val)
        {
            foreach (RibbonTab rTabs in rPrincipal.Tabs)
            {
                foreach (RibbonPanel rPanels in rTabs.Panels)
                {
                    if (Convert.ToInt32(rPanels.Tag) != 4) rPanels.Enabled = _val;
                    else rPanels.Enabled = true;
                }
            }
        }

        private void cerrarFormAbiertos()
        {
            Form[] Formularios = null;
            Formularios = this.MdiChildren;
            foreach (Form Formulario in Formularios) Formulario.Close(); 
        }

        private void openForm(Type frmType)
        {
            foreach (Form f in this.MdiChildren)
            {
                if (f.GetType() == frmType)
                {
                    f.Activate();
                    return;
                }
            }

            Form frm = (Form)Activator.CreateInstance(frmType);
            frm.MdiParent = this;
            frm.Show();
        }

        //By Name
        public void setChildPosition(string frmName)
        {
            try
            {
                Type objType = Type.GetType(frmName);
                Form objForm = (Form)Activator.CreateInstance(objType);

                //Check if there the form is already opened
                bool _OpenForm = true;
                foreach (Form frm in this.MdiChildren)
                {
                    if (frm.Name == Utilitarios.getNombreFrm(frmName))
                    {
                        _OpenForm = false;
                        frm.Focus();
                        return;
                    }
                }

                if (_OpenForm)
                {
                    var _with1 = objForm;
                    _with1.WindowState = FormWindowState.Maximized;
                    _with1.MdiParent = this;
                    _with1.Show();
                    ActivateMdiChild(null);
                    ActivateMdiChild(objForm);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("La opcion seleccionada no esta disponible");
            }
        }

        private void frmPrincipal_Load(object sender, EventArgs e)
        {
            cargarMenuDinamico();
        }        
    }
}
